package com.tencent.iov.cowin.befsale.clue.service.clue.impl;

import com.alibaba.fastjson.JSON;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.AddClueIntentionVehicleRequest;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.ClueFollowUpRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerResponse;
import com.tencent.iov.cowin.befsale.clue.dao.http.log.EtlLogClient;
import com.tencent.iov.cowin.befsale.clue.dao.http.log.request.ClueAddBuriedPointRequest;
import com.tencent.iov.cowin.befsale.clue.dao.http.log.request.ClueRecordBackendRequest;
import com.tencent.iov.cowin.befsale.clue.dao.http.mdquery.MdQueryClient;
import com.tencent.iov.cowin.befsale.clue.dao.http.mdquery.response.DealerResponse;
import com.tencent.iov.cowin.befsale.clue.dao.kafka.response.ScrmLead;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.Clue;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueIntentionVehicle;
import com.tencent.iov.cowin.befsale.clue.service.clue.SyncClueBuriedPintService;
import com.tencent.iov.cowin.befsale.clue.service.cus.CustomerService;
import com.tencent.iov.cowin.befsale.clue.value.enums.ClueEventLogEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.ClueStatsEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.CrmStatusEnum;
import com.tencent.iov.cowin.befsale.clue.value.enums.GenderEnum;
import com.tencent.iov.cowin.befsale.clue.value.utils.CommonUtil;
import com.software.boot.common.entity.Response;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author xiamu
 * @Date 2022/2/12 10:34
 * @Version 1.0
 **/
@Service
@Slf4j
public class SyncClueBuriedPintServiceImpl implements SyncClueBuriedPintService {

    @Autowired
    private MdQueryClient mdQueryClient;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private EtlLogClient etlLogClient;

    @Async
    @Override
    public void clueBuriedPint(ScrmLead request, Clue clue, ClueIntentionVehicle clueVeh) {
        DealerResponse dealerResponse = getDealerResponse(clue);

        //获取客户信息
        CustomerResponse customerResponse = null;
        if (EmptyUtils.isNotEmpty(clue) && EmptyUtils.isNotEmpty(clue.getCusId())) {
            customerResponse = customerService.getDetailByIdAndDealerId(clue.getCusId(), clue.getDealerId());
        }
        if (EmptyUtils.isEmpty(customerResponse)) {
            log.info("[clueBuriedPint] 未获取到客户信息");
            return;
        }

        if (EmptyUtils.isEmpty(request.getLeadStatus())) {
            return;
        }
        //跟进的埋点-代试驾-试乘试驾、交车、实销、战败审核同意
        CrmStatusEnum crmStatusEnum = CrmStatusEnum.getCrmStatusByCode(request.getLeadStatus());
        if (EmptyUtils.isEmpty(crmStatusEnum)) {
            log.info("[clueBuriedPint] crm同步线索状态未找到对应枚举");
            return;
        }
        ClueRecordBackendRequest<Object> params = new ClueRecordBackendRequest();
        params.setUid(customerResponse.getOneId());
        params.setUin(customerResponse.getOneId());
        //9-战败 12-试乘试驾 15-实销 22-已交车
        switch (crmStatusEnum) {
            case crm_status_9:
                Map<String, Object> clueFollowFailAudit = getClueFollowAndFailAudit(request, clueVeh, clue, dealerResponse, customerResponse);
                params.setParams(clueFollowFailAudit);
                params.setEventCode(ClueEventLogEnum.DEFEAT_AUDIT_EVENT.getCode());
                params.setEventName(ClueEventLogEnum.DEFEAT_AUDIT_EVENT.getName());
                params.setEventTime(System.currentTimeMillis());
                break;
            case crm_status_12:
                Map<String, Object> clueTestDrive = getClueFollowAndFailAudit(request, clueVeh, clue, dealerResponse, customerResponse);
                params.setParams(clueTestDrive);
                params.setEventCode(ClueEventLogEnum.TEST_DRIVE_SUCCEED_EVENT.getCode());
                params.setEventName(ClueEventLogEnum.TEST_DRIVE_SUCCEED_EVENT.getName());
                params.setEventTime(System.currentTimeMillis());
                break;
            case crm_status_15:
                //实销
                Map<String, Object> clueActualSales = getClueFollowAndFailAudit(request, clueVeh, clue, dealerResponse, customerResponse);
                params.setParams(clueActualSales);
                params.setEventCode(ClueEventLogEnum.ACTUAL_SALES_EVENT.getCode());
                params.setEventName(ClueEventLogEnum.ACTUAL_SALES_EVENT.getName());
                params.setEventTime(System.currentTimeMillis());
                break;
            case crm_status_22:
                //交车
                Map<String, Object> clueDelivery = getClueFollowAndFailAudit(request, clueVeh, clue, dealerResponse, customerResponse);
                params.setParams(clueDelivery);
                params.setEventCode(ClueEventLogEnum.DELIVERY_EVENT.getCode());
                params.setEventName(ClueEventLogEnum.DELIVERY_EVENT.getName());
                params.setEventTime(System.currentTimeMillis());
                break;
            default:
                break;
        }
        log.info("线索跟进进行埋点，调用参数：【{}】", JSON.toJSONString(params));
        Response response = etlLogClient.backend(params);
        log.info("线索跟进进行埋点，返回：【{}】", JSON.toJSONString(response));
    }

    private DealerResponse getDealerResponse(Clue clue) {
        Integer dealerId = CommonUtil.getIntegerByObj(clue.getDealerId());
        if (EmptyUtils.isEmpty(dealerId) || dealerId <= 0) {
            return null;
        }
        //获取经销商信息
        Response<DealerResponse> mdDealerResponse = mdQueryClient.getDetailById(dealerId);
        if (EmptyUtils.isNotEmpty(mdDealerResponse.getData())) {
            return mdDealerResponse.getData();
        }
        return null;
    }

    private Map<String, Object> getClueFollowAndFailAudit(ScrmLead request, ClueIntentionVehicle clueVeh, Clue clue, DealerResponse dealerResponse, CustomerResponse customerResponse) {
        Map<String, Object> paramsMap = new HashMap<>(16);
        //客户基本信息
        if (EmptyUtils.isNotEmpty(customerResponse)) {
            paramsMap.put("uid", customerResponse.getOneId());
        }

        //线索状态
        if (EmptyUtils.isNotEmpty(clue.getStatus())) {
            String clueStatsName = ClueStatsEnum.getDictionaries(clue.getStatus());
            paramsMap.put("clueStatus", clue.getStatus());
            paramsMap.put("clueStatusName", clueStatsName);
        }

        //意向
        if (EmptyUtils.isNotEmpty(clueVeh) && EmptyUtils.isNotEmpty(clueVeh.getModelId())) {
            paramsMap.put("modelId", clueVeh.getModelName());
        }

        //经销商信息
        if (EmptyUtils.isNotEmpty(dealerResponse)) {
            paramsMap.put("dealerId", dealerResponse.getId());
            paramsMap.put("dealerName", dealerResponse.getName());
        }
        return paramsMap;
    }

    @Async
    @Override
    public void addClueBuriedPint(ClueFollowUpRequest request, Clue clue, CustomerResponse customerResponse) {
        ClueAddBuriedPointRequest addBuriedPointRequest = new ClueAddBuriedPointRequest();
        if (EmptyUtils.isEmpty(customerResponse) || EmptyUtils.isEmpty(clue) || EmptyUtils.isEmpty(request) || EmptyUtils.isEmpty(customerResponse.getOneId())) {
            log.info("线索新增进行埋点，传入参数错误");
            return;
        }
        //客户基本信息
        if (EmptyUtils.isNotEmpty(customerResponse)) {
            BeanUtil.copy(customerResponse, addBuriedPointRequest);
            GenderEnum genderEnum = GenderEnum.getByCode(customerResponse.getGender());
            if (EmptyUtils.isNotEmpty(genderEnum)) {
                addBuriedPointRequest.setGender(genderEnum.getName());
            }
            addBuriedPointRequest.setUid(customerResponse.getOneId());
        }

        //线索状态
        if (EmptyUtils.isNotEmpty(clue.getStatus())) {
            String clueStatsName = ClueStatsEnum.getDictionaries(clue.getStatus());
            addBuriedPointRequest.setClueStatus(clue.getStatus());
            addBuriedPointRequest.setClueStatusName(clueStatsName);
        }

        //意向
        if (EmptyUtils.isNotEmpty(request) && EmptyUtils.isNotEmpty(request.getAddVehicleList())) {
            AddClueIntentionVehicleRequest vehicleRequest = request.getAddVehicleList().get(0);
            addBuriedPointRequest.setModelId(vehicleRequest.getModelName());
        }

        //经销商信息
        DealerResponse dealerResponse = getDealerResponse(clue);
        if (EmptyUtils.isNotEmpty(dealerResponse)) {
            addBuriedPointRequest.setDealerId(clue.getDealerId());
            addBuriedPointRequest.setDealerName(dealerResponse.getName());
        }

        ClueRecordBackendRequest<Object> params = new ClueRecordBackendRequest();
        params.setParams(addBuriedPointRequest);
        params.setUid(customerResponse.getOneId());
        params.setUin(customerResponse.getOneId());
        params.setEventCode(ClueEventLogEnum.CLUE_ADD_SUCCEED_EVENT.getCode());
        params.setEventName(ClueEventLogEnum.CLUE_ADD_SUCCEED_EVENT.getName());
        params.setEventTime(System.currentTimeMillis());
        log.info("线索新增进行埋点，调用参数：【{}】", JSON.toJSONString(params));
        Response response = etlLogClient.backend(params);
        log.info("线索新增进行埋点，返回：【{}】", JSON.toJSONString(response));
    }
}
